package com.tee.live.im.core.server.handler;

import com.tee.im.constants.ImMsgCodeEnum;
import com.tee.live.im.core.server.common.ImMsg;
import com.tee.live.im.core.server.factory.ImHandlerFactory;
import io.netty.channel.ChannelHandlerContext;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息处理工厂实现类
 * @author tee
 * @date 2025/10/22
 */
@Component
public class ImHandlerFactoryImpl implements ImHandlerFactory , InitializingBean {

    @Resource
    private ApplicationContext applicationContext;

    private static Map<Integer, SimplyHandler> simplyHandlerMap = new HashMap<>();


    @Override
    public void doMsgHandler(ChannelHandlerContext ctx, ImMsg msg) {
        SimplyHandler handler = simplyHandlerMap.get(msg.getCode());
        handler.handler(ctx, msg);
    }

    @Override public void afterPropertiesSet() throws Exception {
        simplyHandlerMap.put(ImMsgCodeEnum.IM_BIZ_MSG.getCode(), applicationContext.getBean(BizImMsgHandler.class));
        simplyHandlerMap.put(ImMsgCodeEnum.IM_HEARTBEAT_MSG.getCode(), applicationContext.getBean(HeartBeatImMsgHandler.class));
        simplyHandlerMap.put(ImMsgCodeEnum.IM_LOGIN_MSG.getCode(), applicationContext.getBean(LoginImMsgHandler.class));
        simplyHandlerMap.put(ImMsgCodeEnum.IM_LOGOUT_MSG.getCode(), applicationContext.getBean(LogoutImMsgHandler.class));
    }

}
